
package com.wyzz.hr.action;

import java.text.SimpleDateFormat;
import java.util.List;

import com.wyzz.hr.bo.IDeptHistBo;
import com.wyzz.hr.bo.IEmpHistOrgBo;
import com.wyzz.hr.bo.IPositionBaseHistBo;
import com.wyzz.hr.pojo.Department;
import com.wyzz.hr.pojo.Depthist;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.spring.SpringBeanFactory;


public class AssembleHist extends AssembleDeptList {

	public AssembleHist() {
		dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	}

	protected void getDeptList() {
		IDeptHistBo deptHistBo = (IDeptHistBo) SpringBeanFactory
				.getBean("deptHistBo");
		List dhList = deptHistBo.getDhsBeforeQueryDate(queryDate);
		for (int i = 0; i < dhList.size(); i++) {
			Depthist dh = (Depthist) dhList.get(i);
			allResultList.add(dh.assembleDeptByDh());
		}

		prepareInitialDeptList();
	}

	protected void setActualNum() {
		IEmpHistOrgBo empHistOrgBo = (IEmpHistOrgBo) SpringBeanFactory
				.getBean("empHistOrgBo");
		String dateString = dateFormat.format(queryDate);
		StringBuffer hql = new StringBuffer();
		hql
				.append("select e.ehoDeptNo,count(*) from EmpHistOrg e where e.ehoValidFrom<='");
		hql.append((new StringBuilder()).append(dateString).append("' ")
				.toString());
		hql.append((new StringBuilder()).append("and (e.ehoValidTo>='").append(
				dateString).append("' or e.ehoValidTo is null) ").toString());
		hql.append("group by e.ehoDeptNo");
		List deptActualNumList = empHistOrgBo.exeHqlList(hql.toString());
		label0: for (int i = 0; i < deptList.size(); i++) {
			Department dept = (Department) deptList.get(i);
			String deptId = dept.getId();
			int j = 0;
			do {
				if (j >= deptActualNumList.size())
					continue label0;
				Object deptActualNum[] = (Object[]) (Object[]) deptActualNumList
						.get(j);
				String turnId = ((Department) deptActualNum[0]).getId();
				if (deptId.equals(turnId)) {
					dept.setActualNum(Integer.valueOf(((Long) deptActualNum[1])
							.intValue()));
					continue label0;
				}
				j++;
			} while (true);
		}

	}

	protected void setOrderNum() {
		IPositionBaseHistBo pbhBo = (IPositionBaseHistBo) SpringBeanFactory
				.getBean("positionBaseHistBo");
		String dateString = dateFormat.format(queryDate);
		StringBuffer hql = new StringBuffer();
		hql
				.append("select e.pbhiDeptId,sum(e.pbhiMaxEmployee) from PositionBaseHist e where e.pbhiPbStatus=1 ");
		hql.append((new StringBuilder()).append("and e.pbhiValidFrom <='")
				.append(dateString).append("'").toString());
		hql.append((new StringBuilder()).append("and (e.pbhiValidTo>='")
				.append(dateString).append("' or e.pbhiValidTo is null) ")
				.toString());
		hql.append("group by e.pbhiDeptId");
		List deptOrderNumList = pbhBo.exeHqlList(hql.toString());
		label0: for (int i = 0; i < deptList.size(); i++) {
			Department dept = (Department) deptList.get(i);
			String deptId = dept.getId();
			int j = 0;
			do {
				if (j >= deptOrderNumList.size())
					continue label0;
				Object deptOrderNum[] = (Object[]) (Object[]) deptOrderNumList
						.get(j);
				String turnId = ((Department) deptOrderNum[0]).getId();
				if (deptId.equals(turnId)) {
					dept.setOrderNum(Integer.valueOf(((Long) deptOrderNum[1])
							.intValue()));
					continue label0;
				}
				j++;
			} while (true);
		}

	}

	protected void setDeptHead() {
		IPositionBaseHistBo pbhBo = (IPositionBaseHistBo) SpringBeanFactory
				.getBean("positionBaseHistBo");
		String dateString = dateFormat.format(queryDate);
		StringBuffer hql = new StringBuffer();
		hql
				.append("select e.pbhiDeptId,p.empName from PositionBaseHist e ,EmpHistOrg o join o.ehoEmpNo p where e.pbhiPbId=p.empPbNo and e.pbhiInCharge=1 and e.pbhiPbStatus=1");
		hql.append((new StringBuilder()).append("and e.pbhiValidFrom <='")
				.append(dateString).append("'").toString());
		hql.append((new StringBuilder()).append("and (e.pbhiValidTo>='")
				.append(dateString).append("' or e.pbhiValidTo is null) ")
				.toString());
		hql.append("group by e.pbhiDeptId");
		List deptHeadList = pbhBo.exeHqlList(hql.toString());
		label0: for (int i = 0; i < deptList.size(); i++) {
			Department dept = (Department) deptList.get(i);
			String deptId = dept.getId();
			int j = 0;
			do {
				if (j >= deptHeadList.size())
					continue label0;
				Object deptHead[] = (Object[]) (Object[]) deptHeadList.get(j);
				String turnId = ((Department) deptHead[0]).getId();
				if (deptId.equals(turnId)) {
					Employee emp = new Employee();
					emp.setEmpName((String) deptHead[1]);
					dept.setDeptHead(emp);
					continue label0;
				}
				j++;
			} while (true);
		}

	}

	private SimpleDateFormat dateFormat;
}
